
package com.csc440.team2.activity;

import java.util.List;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


import com.csc440.team2.SmilConstants;
import com.csc440.team2.database.entities.FolderType;
import com.csc440.team2.database.entities.Message;
import com.csc440.team2.smil.SMILComponent;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILReadException;


public class Components extends ListActivity
{
    private SMILMessage message;


    @Override
    public void onCreate(Bundle savedIntanceState)
    {
        super.onCreate(savedIntanceState);

        Bundle bundle = getIntent().getExtras();

        if (bundle != null)
        {
            message = (SMILMessage) bundle.getSerializable(SmilConstants.MESSAGE);

            LinearLayout buttonLayout = new LinearLayout(this);

            Button button = new Button(this);
            button.setText("Add Items");
            button.setGravity(Gravity.CENTER);
            button.setOnClickListener(new OnClickListener()
            {

                public void onClick(View v)
                {
                    Intent in = new Intent(Components.this, AddComponents.class);
                    in.putExtra(SmilConstants.MESSAGE, message);
                    Components.this.startActivityForResult(in, 1);
                }

            });

            EditText toView = new EditText(this);
            toView.setText("To");

            Button sendButton = new Button(this);
            sendButton.setText("Send");
            sendButton.setGravity(Gravity.CENTER);
            sendButton.setOnClickListener(new OnClickListener()
            {

                public void onClick(View v)
                {
                    saveMessage();
                    sendMessage();
                    finish();
                }

            });

            buttonLayout.addView(button, new ViewGroup.LayoutParams(155, -2));
            buttonLayout.addView(sendButton, new ViewGroup.LayoutParams(155, -2));

            ListView lv = getListView();
            lv.addHeaderView(toView);
            lv.addHeaderView(buttonLayout);

            List<SMILComponent> list = getComponents(message);
            if (list.size() == 0)
                setListAdapter(new ArrayAdapter<String>(this, R.layout.components, new String[] {
                    "No components to display"
                }));
            else
                setListAdapter(new ArrayAdapter<SMILComponent>(this, R.layout.components, getComponents(message)));

            lv.setTextFilterEnabled(true);

            lv.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent, View clickedView, int position, long clickedItemId)
                {
                    // have to set the new view
                    if (position == SmilConstants.SELECT_ADD_COMPONENT)
                    {

                        Intent in = new Intent(Components.this, AddComponents.class);
                        Components.this.startActivity(in);
                    }
                    else if (position == SmilConstants.SELECT_REMOVE_COMPONENT)
                    {
                        Intent in = new Intent(Components.this, RemoveComponents.class);
                        Components.this.startActivity(in);
                    }
                }
            });
        }
    }


    private void saveMessage()
    {
        try
        {
            Message dbMessage;

            dbMessage = new Message(message.getXml(), FolderType.SENT, true, "");

            if (dbMessage.save(this))
                Log.i("Save", "Message Saved");
            else
                Log.i("Save", "Failed to save message");

        }
        catch (SMILReadException e)
        {
            Log.e("Save", "Could not save message", e);
        }
    }


    private void sendMessage()
    {
    // TODO send me!!!!
    }


    private List<SMILComponent> getComponents(SMILMessage message)
    {
        return message.getComponentList();
    }


    @Override
    protected void onSaveInstanceState(Bundle bundle)
    {
        bundle.putSerializable(SmilConstants.MESSAGE, message);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        SMILMessage returnedMessage = (SMILMessage) data.getExtras().get(SmilConstants.MESSAGE);
        message = returnedMessage;
        setListAdapter(new ArrayAdapter<SMILComponent>(this, R.layout.components, getComponents(message)));
    }
}
